package com.fjec.market.schedule.handler;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fjec.common.Const;
import com.fjec.common.util.NumberUtil;
import com.fjec.market.entity.BizShopSeq;
import com.fjec.market.entity.InfoPurchase;
import com.fjec.market.helper.MysqlShopNumHelper;
import com.fjec.market.service.IBizShopSeqService;
import com.fjec.market.service.IInfoPurchaseService;
import com.xxl.job.core.biz.model.ReturnT;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@Component
public class PurchaseShopNumHandler {

    @Resource
    private IInfoPurchaseService purchaseService;

    @Resource
    private MysqlShopNumHelper mysqlShopNumHelper;

    public int handle(int limit, int minutes) {
        int num = 0;
        List<InfoPurchase> entityList = purchaseService.getPurchaseForShopNumGen(limit, minutes);
        for (InfoPurchase entity : entityList) {
            // 生成shopNum
            try {
                Long genNum = mysqlShopNumHelper.genShopNum(Const.MARKET_CODE_PURCHASE);
                if (genNum != null) {
                    String strNum = String.format("%d%06d", Const.MARKET_CODE_PURCHASE, genNum);
                    UpdateWrapper<InfoPurchase> updateWrapper = new UpdateWrapper<>();
                    updateWrapper.eq("Id", entity.getId());
                    entity = new InfoPurchase();
                    entity.setShopNum(Long.valueOf(strNum));
                    purchaseService.update(entity, updateWrapper);
                    num++;
                }
            }catch (Exception ex) {
                log.error(ex.getMessage(), ex);
            }
        }
        return num;
    }



}
